﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using KPIS.GERP.Models.SYS.ViewModel;

namespace KPIS.GERP.WEB.GAMS.Class
{
	public static class GimsConfigReader
	{

		#region GIMS Config Strong Type Reader

		public static string SiteMenuXml
		{
			get { return GetAppSetting("SiteMenuXml"); }
		}

		public static string LoginMainTitle
		{
			get { return GetAppSetting("LoginMainTitle"); }
		}

		public static string LoginSubTitle
		{
			get { return GetAppSetting("LoginSubTitle"); }
		}

		public static string LoginThirdTitle
		{
			get { return GetAppSetting("LoginThirdTitle"); }
		}

		public static string LoginLogoPath
		{
			get { return GetAppSetting("LoginLogoPath"); }
		}

		public static string GimsFileFolder
		{
			get { return GetAppSetting("GimsFileFolder"); }
		}

		public static string SystemName
		{
			get { return GetAppSetting("SystemName"); }
		}

		public static string GERPContextConnectionString
		{
			get { return GetConnectionString("GERPContext"); }
		}

		public static bool CacheMenuInMemory
		{
			get { return Models.Class.ConvertUtility.ConvertOrDefaultStringToBool(GetAppSetting("CacheMenuInMemory")); }
		}

        public static WhenGoToLoginPageEnum WhenGoToLoginPage
        {
            get
            {
                string whenGoToLoginPageWebConfig = GetAppSetting("WhenGoToLoginPage");

                if (Models.Class.CompareUtility.IsObjectToStringEqual(whenGoToLoginPageWebConfig, WhenGoToLoginPageEnum.LogOff))
                {
                    return WhenGoToLoginPageEnum.LogOff;
                }
                else if (Models.Class.CompareUtility.IsObjectToStringEqual(whenGoToLoginPageWebConfig, WhenGoToLoginPageEnum.StayAtLogin))
                {
                    return WhenGoToLoginPageEnum.StayAtLogin;
                }
                else if (Models.Class.CompareUtility.IsObjectToStringEqual(whenGoToLoginPageWebConfig, WhenGoToLoginPageEnum.RedirectToHome))
                {
                    return WhenGoToLoginPageEnum.RedirectToHome;
                }
                else
                {
                    // return default
                    return WhenGoToLoginPageEnum.StayAtLogin;
                }
            }
        }

        public enum WhenGoToLoginPageEnum
        {
            LogOff,
            StayAtLogin,
            RedirectToHome
        }

		#endregion


		#region General Use

		public static string GetAppSetting(string entryName, string defaultValue = null)
		{
			try
			{
				return ConfigurationManager.AppSettings[entryName];
			}
			catch (Exception ex)
			{
				Models.Class.Logger.ArgumentNullException("Web.Config entry not found", string.Format("Web.Config of Gims entry [{0}] is not set. Error below\r{1}", entryName, ex.Message));
				return defaultValue;
			}
		}

		public static string GetConnectionString(string entryName)
		{
			try
			{
				return ConfigurationManager.ConnectionStrings[entryName].ConnectionString;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		#endregion
	}
}